<template>
  <ul class="todo-list">
    <!-- completed: 完成的类名 -->
    <li
      :class="{ completed: item.state }"
      v-for="(item, index) in showList"
      :key="key(index)"
    >
      <div class="view">
        <input
          class="toggle"
          type="checkbox"
          @change="changeState(index)"
          :checked="item.state"
        />
        <label>{{ item.todo }}</label>
        <button class="destroy" @click="deleteFn(index)"></button>
      </div>
    </li>
  </ul>
</template>

<script>
export default {
  computed: {
    todoList() {
      return this.$store.state.todoList;
    },
    done() {
      return this.$store.getters.done;
    },
    undone() {
      return this.$store.getters.undone;
    },
    select() {
      return this.$store.state.select;
    },
    showList() {
      if (this.select === 1) {
        return this.todoList;
      } else if (this.select == 2) {
        return this.undone;
      } else {
        return this.done;
      }
    },
    doneIndex() {
      return this.$store.getters.doneIndex;
    },
    undoneIndex() {
      return this.$store.getters.undoneIndex;
    },
  },
  methods: {
    changeState(index) {
      this.$store.dispatch("changeState", index);
    },
    key(index) {
      if (this.select == 1) {
        return index;
      } else if (this.select == 2) {
        return this.undoneIndex[index];
      } else if (this.select == 3) {
        return this.doneIndex[index];
      }
    },
    deleteFn(index) {
      this.$store.dispatch("chooseDelete", index);
    },
  },
};
</script>
